package com.ums.upos.service;

import android.app.ums.IUms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.OperationCanceledException;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.ums.upos.uapi.ServiceResult;
import com.ums.upos.uapi.device.logrecorder.LogRecorder;
import com.ums.upos.uapi.engine.DeviceDetailConstrants;

import net.sourceforge.zbar.android.PreferencesActivity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by 白小峰 on 2018/6/20 0020.
 */

public class LogRecorderBinder extends LogRecorder.Stub{

    private static final String TAG = "LogRecorderService";
    private Context mContext;
    private Logger log;
    private final String START_BROADCASE = "com.android.umslog.UMSLOG_START";
    private final String STOP_BROADCASE = "com.android.umslog.UMSLOG_STOP";

    LogRecorderBinder(Context context){
        log = LoggerFactory.getLogger(TAG);
        this.mContext = context;

    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            log.debug("action {}", action);
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
            if (START_BROADCASE.equals(action)) {
                log.info("START_BROADCASE");
                editor.putInt("logRecorderStatus",0);
            }
            if (STOP_BROADCASE.equals(action)) {
                log.info("STOP_BROADCASE");
                editor.putInt("logRecorderStatus",1);
            }
            editor.commit();
            context.unregisterReceiver(mReceiver);

        }

    };

    @Override
    public int getStatus() throws RemoteException {
        log.info("call getStatus");
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        int status = preferences.getInt("logRecorderStatus",1);
        log.info("call status:"+status);
        return status;
    }

    @Override
    public int openRecorder() throws RemoteException {
        log.info("call openRecorder");
        IntentFilter filter = new IntentFilter();
        filter.addAction(START_BROADCASE);
        mContext.registerReceiver(mReceiver, filter);
        try{
            Intent intent = new Intent();
            intent.setAction(START_BROADCASE);
            mContext.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
            return ServiceResult.LogRecorder_Open_Fail;
        }

        return 0;
    }

    @Override
    public int closeRecorder() throws RemoteException {
        log.info("call closeRecorder");
        IntentFilter filter = new IntentFilter();
        filter.addAction(STOP_BROADCASE);
        mContext.registerReceiver(mReceiver, filter);
       try{
            Intent intent = new Intent();
            intent.setAction(STOP_BROADCASE);
            mContext.sendBroadcast(intent);
       } catch (Exception e) {
            e.printStackTrace();
            return ServiceResult.LogRecorder_Close_Fail;
       }

        return 0;
    }

    @Override
    public String getLogFilePath() throws RemoteException {
        log.info("call getLogFilePath");
        IUms ums = (IUms) mContext.getSystemService("ums_device_service");
        String logPath;
        try{
             logPath = ums.getLogPath();
             return logPath;
        } catch (RemoteException e) {
             e.printStackTrace();
             return ServiceResult.LogRecorder_GetLogFilePath_Error+"";
        }

    }
}
